package com.zdj.presentation.modules.demo.vms;

import android.databinding.ObservableField;

import com.zdj.data.entity.TestUserEntity;
import com.zdj.data.net.DefaultSubscribe;
import com.zdj.data.net.NetWorks;
import com.zdj.data.response.TestResponse;
import com.zdj.presentation.base.BaseViewModel;
import com.zdj.presentation.mappers.IEntity2Model;
import com.zdj.presentation.modules.demo.TestGsonActivity;
import com.zdj.presentation.modules.demo.handler.TestUserEntityHandlers;
import com.zdj.presentation.modules.demo.models.TestUserModel;
import com.zdj.utils.MyLogUtil;

import java.util.List;

import retrofit2.adapter.rxjava.Result;

/**
 * Created by zdj on 16-10-6.
 */

public class TestGsonVm extends BaseViewModel<TestGsonActivity> implements TestUserEntityHandlers {

    public final ObservableField<TestUserModel> userObs = new ObservableField<>();

    TUE2TUM mapper = new TUE2TUM();


    public TestGsonVm(TestGsonActivity testGsonActivity) {
        super(testGsonActivity);
    }

    @Command
    public void getSchoolList(){

        NetWorks.loadSchoolList("大学", new DefaultSubscribe<Result<TestResponse>>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
                MyLogUtil.d("TestGsonVm", "onCompleted: \n" + "请求完成");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                MyLogUtil.e("TestGsonVm", "onError: \n" + "请求出错");
            }

            @Override
            public void onNext(Result<TestResponse> testResponseResult) {
                super.onNext(testResponseResult);
                MyLogUtil.d("TestGsonVm", "onNext: \n" + testResponseResult.response().body().toString());
            }
        });

    }

    @Command
    public void getUserModel(){
        // TODO: 2017/1/20 获取userBean
        /*NetWorks.loadBean(new DefaultSubscribe<Result<TestUserEntity>>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
                MyLogUtil.e(TAG, "onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                MyLogUtil.e(TAG, "onError: " + e.toString());
            }

            @Override
            public void onNext(Result<TestUserEntity> result) {
                super.onNext(result);
                MyLogUtil.e(TAG, "onNext: " + result.toString());
                userObs.set(mapper.one2one(result.response().body()));
            }
        });*/
    }

    @Override
    public void onGetUserEntityBtnClick() {
        t.showToast("按钮被点击了");
        getUserModel();
    }

    @Override
    public void onGetSchoolListBtnClick() {
        getSchoolList();
    }

    private static class TUE2TUM implements IEntity2Model<TestUserEntity,TestUserModel>{

        @Override
        public TestUserModel one2one(TestUserEntity testUserEntity) {
            TestUserModel testUserModel = new TestUserModel();
            testUserModel.age = testUserEntity.age;
            testUserModel.emailAddress = testUserEntity.emailAddress;
            testUserModel.name = testUserEntity.name;
            return testUserModel;
        }

        @Override
        public List<TestUserModel> more2more(List<TestUserEntity> testUserEntities) {
            return null;
        }
    }

}